package ru.nsu.expertsystem.chucknorris;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import ru.nsu.expertsystem.chucknorris.xml_parser.XmlParser;

/**
 * This is a sample class to launch a rule.
 */
public class ChuckNorrisFactsGenerator {

    public static final void main(String[] args) {
        try {
        	final Logger logger = new ConsoleLogger();
        	
        	if(args.length < 1) {
        		logger.error("input file not specified");
        		return;
        	}
        	
        	final String inputFileName = args[0];
        	InputStream inputStream;
        	try {
        		inputStream = new FileInputStream(inputFileName);
        	} catch (IOException e) {
				logger.error("couldn't open input file: " + inputFileName);
				return;
			}
        	
        	final XmlParser parser = new XmlParser(ChuckNorrisFactsGenerator.class.getPackage().getName(), logger);
        	
        	try {
        		parser.parse(inputStream);
        	} catch (Exception e) {
        		logger.error("couldn't parse input file: " + inputFileName, e);
        		return;
			}
        	
        	try {
        		inputStream.close();
        	} catch (Exception e) {
			}
        	
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            
            // don't create this dump files!
            // KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");

            // go !
            
            // add logger to console
            LoggingWorkerMemoryEventListener loggingListener = new LoggingWorkerMemoryEventListener();
            ksession.addEventListener(loggingListener);            
            
            for(Object object : parser.getObjects()) {
            	ksession.insert(object);
            }
            
            for(Predicate predicate : parser.getPredicates()) {
            	ksession.insert(predicate);
            }
            
            ksession.fireAllRules();
            
            // don't create this dump files!
            // logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }

    public static class Message {

        public static final int HELLO = 0;
        public static final int GOODBYE = 1;

        private String message;

        private int status;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

    }

}
